package com.ukao.cashregister.ui.arriveShop;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseFragment;
import com.ukao.cashregister.consts.SaveParamets;
import com.ukao.cashregister.view.ArriveShopHomeView;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by ukaodev on 2018/6/28.
 * 到店首页framgment
 */

public class ArriveShopHomeFragment extends BaseFragment implements ArriveShopHomeView {

    Unbinder unbinder;

    private ArriveShopFragment batchArriverShowFragment, orderArriverShowFragment;
    private ClothingArriveShopFragment clothingArriveShopFragment;

    public static ArriveShopHomeFragment newInstance() {
        ArriveShopHomeFragment arriveShopHomeFragment = new ArriveShopHomeFragment();
        return arriveShopHomeFragment;
    }

    @Override
    protected void initView() {
        ArriveShopFragment firstFragment = findFragment(ArriveShopFragment.class);
        if (firstFragment == null) {
            batchArriverShowFragment = ArriveShopFragment.newInstance(false);
            orderArriverShowFragment = ArriveShopFragment.newInstance(true);
            clothingArriveShopFragment = ClothingArriveShopFragment.newInstance();
            //获取加载默认的到店界面
            loadMultipleRootFragment(R.id.home_arrive_show_layout, SaveParamets.getsaveArriveShopPosition(),
                    batchArriverShowFragment,
                    orderArriverShowFragment, clothingArriveShopFragment);
        } else {
            batchArriverShowFragment = firstFragment;
            orderArriverShowFragment = findFragment(ArriveShopFragment.class);
            clothingArriveShopFragment = findFragment(ClothingArriveShopFragment.class);
        }
        batchArriverShowFragment.setArriveShopHomeView(this);
        orderArriverShowFragment.setArriveShopHomeView(this);
        clothingArriveShopFragment.setArriveShopHomeView(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_arrive_shop, null);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private SupportFragment resultFragment(int position) {
        SupportFragment supportFragment = null;
        switch (position) {
            case 0:
                supportFragment = batchArriverShowFragment;
                break;
            case 1:
                supportFragment = orderArriverShowFragment;
                break;
            case 2:
                supportFragment = clothingArriveShopFragment;
                break;
        }
        return supportFragment;
    }

    @Override
    public void showPositionFragment(int showPosition, int hidePosition) {
        showHideFragment(resultFragment(showPosition), resultFragment(hidePosition));
    }
}
